﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using GD.ModelContest.Common;
using GD.ModelContest.Model;
using GD.ModelContest.Service;

namespace GD.ModelContest.Web
{
    public partial class UserView : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!String.IsNullOrEmpty(Request.QueryString["UserId"]))
            {
                DataBind_UserInfo();
                DataBind_Video();
                DataBind_Album();
                DataBind_Msg(0);
                if (!Page.IsPostBack)
                {
                    if (Session["User"] != null)
                    {
                        if (Session["User"].ToString() == Request.QueryString["UserId"])
                        {
                            lbtnModifyUserInfo.Visible = true;
                        }
                    }
                }
            }
        }

        protected void DataBind_UserInfo()
        {
            mc_user mcUser = ServiceLocator.IUserService.GetUserById(int.Parse(Request.QueryString["UserId"]));
            mc_photo mcPhoto = ServiceLocator.IUserService.GetHeadPhotoByUserId(int.Parse(Request.QueryString["UserId"]));
            mc_vote mcVote = ServiceLocator.IUserService.GetVoteByUserId(int.Parse(Request.QueryString["UserId"]));
            if (mcUser != null)
            {
                imgHeadPhoto.ImageUrl = "/ImgUpload/" + mcPhoto.photoName;
                ltVoteNum.Text = mcVote.voteNum.ToString();
                ltName.Text = mcUser.name;
                ltPinCode.Text = mcUser.phoneCode;
                ltItem.Text = mcUser.items;
                ltHeight.Text = mcUser.bodyHeight;
                ltWeight.Text = mcUser.bodyWeight;
                ltSpecialty.Text = mcUser.specialty;
                ltShoeSize.Text = mcUser.shoeSize;
                ltShoulderWidth.Text = mcUser.shoulderWidth;
                ltEducation.Text = mcUser.education;
                ltBlood.Text = mcUser.bloodType;
                ltBustWaistHip.Text = mcUser.bust + " * " + mcUser.waist + " * " + mcUser.hip;
                ltIntro.Text = mcUser.intro;
            }
        }
        protected void DataBind_Video()
        {
            this.dlVideoList.DataSource=ServiceLocator.IUserService.GetVideoCollection(int.Parse(Request.QueryString["UserId"]));
            this.dlVideoList.DataBind();
        }

        protected void DataBind_Album()
        {
            this.rpPhotoList.DataSource = ServiceLocator.IUserService.GetPhotoCollection(int.Parse(Request.QueryString["UserId"]));
            this.rpPhotoList.DataBind();
        }

        protected void DataBind_Msg(int recordcount)
        {
            if (recordcount == 0)
            {
                this.AspNetPager_Msg.CurrentPageIndex = 1;
            }
            int count = recordcount;
            int pageIndex = this.AspNetPager_Msg.CurrentPageIndex;
            int pageSize = this.AspNetPager_Msg.PageSize;

            this.gvMsgList.DataSource = ServiceLocator.IMessageService.GetMessageCollection(int.Parse(Request.QueryString["UserId"]),pageIndex, pageSize, ref count);
            this.gvMsgList.DataBind();
            this.AspNetPager_Msg.RecordCount = count;
        }

        protected void dlVideoList_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                mc_video video = (mc_video)e.Item.DataItem;

                Literal ltVideoStr = (Literal)e.Item.FindControl("ltVideoStr");
                ltVideoStr.Text = video.videoPath.Replace("width=\"480\" height=\"400\"","width=\"210\" height=\"200\"");
            }
        }

        protected void rpPhotoList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                mc_photo photo = (mc_photo)e.Item.DataItem;

                Literal ltPhoto = (Literal)e.Item.FindControl("ltPhoto");
                ltPhoto.Text = "<a href=\"/ImgUpload/"+ photo.photoName +"\" title=\""+ photo.photoId +"\" class=\"thickbox\" rel=\"gallery-plants\"><img src=\"/ImgUpload/"+ photo.photoName +"\" alt=\"" + photo.photoId + "\" width=\"150px\" height=\"171px\" /></a>";
            }
        }

        protected void gvMsgList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                mc_message mcMessage = (mc_message)e.Row.DataItem;
                Literal ltNickName = (Literal)e.Row.FindControl("ltNickName");
                ltNickName.Text = mcMessage.nickName;
                Literal ltMsgContent = (Literal)e.Row.FindControl("ltMsgContent");
                if (mcMessage.msgContent.Length > 25)
                {
                    ltMsgContent.Text = CommonMethod.SubString(mcMessage.msgContent, 25, "..");
                }
                else
                {
                    ltMsgContent.Text = mcMessage.msgContent;
                }

                Literal ltCreateTime = (Literal)e.Row.FindControl("ltCreateTime");
                ltCreateTime.Text = mcMessage.createTime.ToShortDateString();
            }
        }

        protected void AspNetPager_Msg_PageChanged(object sender, EventArgs e)
        {
            DataBind_Msg(this.AspNetPager_Msg.RecordCount);
        }

        protected void lbtnModifyUserInfo_Click(object sender, EventArgs e)
        {
            lbtnModifyUserInfo.PostBackUrl = "ModifyUserInfo.aspx?UserId=" + Request.QueryString["UserId"];
        }

        protected void btnVoteMe_Click(object sender, EventArgs e)
        {
            string ip = Request.UserHostAddress.ToString();
            string startTime = DateTime.Now.Year + "-" + DateTime.Now.Month+ "-" + DateTime.Now.Day +" 0:00:00";
            string endTime = DateTime.Now.Year + "-" + DateTime.Now.Month+ "-" + DateTime.Now.Day +" 23:59:59";
            mc_ip mcIp = ServiceLocator.IUserService.GetIpByTime(ip, startTime, endTime);
            if (mcIp == null)
            {
                mc_vote mcVote = ServiceLocator.IUserService.GetVoteByUserId(int.Parse(Request.QueryString["UserId"]));
                mcVote.voteNum = mcVote.voteNum + 1;
                if (ServiceLocator.IUserService.UpdateVote(mcVote))
                {
                    //if (ServiceLocator.IUserService.GetIp(ip) >0)
                    //{ 
                    //    mc
                    //}
                    JavaScript.Alert("投票成功！", this.Page);

                }
                else
                {
                    JavaScript.Alert("投票失败，一个IP一天只能投票一次。", this.Page);
                }
            }
            else 
            {
                JavaScript.Alert("投票失败，一个IP一天只能投票一次。", this.Page);
            }
        }
    }
}
